<template>
	<view class="content" :style="{backgroundImage: 'url('+imageURL+')'}">
		<view class="box">
			<uni-forms ref="form" :modelValue="formData">
				<uni-forms-item label="用户名" name="formData.name">
					<uni-easyinput type="text" v-model="formData.name" placeholder="请输入用户名" />
				</uni-forms-item>
				<uni-forms-item label="密码" name="formData.pwd">
					<uni-easyinput type="password" v-model="formData.pwd" placeholder="请输入密码" />
				</uni-forms-item>
				<button type="primary" @click="submit">登录</button>
				<button type="primary" @click="goregister">注册</button>
			</uni-forms>
		</view>
	</view>
</template>

<script>
	export default {

		data() {
			return {
				imageURL: "../../static/image/back3.gif",
				formData: {
					name: "",
					pwd: "",
				},
				rules: {
					// 对name字段进行必填验证
					name: {
						// name 字段的校验规则
						rules: [
							// 校验 name 不能为空
							{
								required: true,
								errorMessage: "请填写用户名",
							},
						],
					},
					address: {
						// name 字段的校验规则
						rules: [
							// 校验 name 不能为空
							{
								required: true,
								errorMessage: "请填写密码",
							},
						],
					},
				},
			};
		},
		methods: {
			submit() {
				//重定向
				let _this = this
				//调用接口
				uni.request({
					url: 'http://localhost:8000/api/denglu',
					method: 'POST',
					data: {
						msg: {
							username:_this.formData.name,
							passwrok:_this.formData.pwd
						}
					},
					success(res) {
						//判断后端接口传回数据
						if(res.data.message=="登陆成功"){
							let key={
								username:_this.formData.name,
								passwrok:_this.formData.pwd,
								exports:res.data.exports,
								main:res.data.timese
							}
							let keyindex=JSON.stringify(key)
							let token =encodeURIComponent(keyindex)
							uni.setStorageSync("token", token)
							uni.redirectTo({
								url: "/pages/Home/Home"
							})
						}else if(res.data.message=="用户名不存在"){
							uni.showToast({
								title:"用户名不存在",
								icon:"error",
							})
							
						}else if(res.data.message=="密码错误"){
							uni.showToast({
								title:"密码不正确",
								icon:"error"
							})
						}
					}
				})
			},
			goregister(){
				uni.navigateTo({
					url:"/pages/Register/register"
				})
			}
		},
	};
</script>

<style lang="scss">
	.content {
		width: 100vw;
		height: 100vh;
		display: flex;
		flex-direction: column;
		background-size: 100%;

	}

	.box {
		width: 90%;
		height: auto;
		position: fixed;
		top: 25%;
		left: 5%;

		button {
			margin-top: 20rpx;
		}

	}
</style>